﻿using System;
using Microsoft.Owin.Hosting;
using System.Threading;
using Sustainalytics.Utils.Configuration;
using Sustainalytics.ReportGenerator.BL;

namespace Sustainalytics.PdfGeneration.Service
{
    public static class ProgramConsole
    {
        public static void Start()
        {
            try
            {
                var url = HostConfig.GetHostUrl();
                using (WebApp.Start<Startup>(url))
                {
                    Console.WriteLine("Started on: " + url);
                    Console.ReadKey();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Startup.IsGenerationRunning = false;
                Startup.Token.Dispose();
                Startup.Token = new CancellationTokenSource();
                Startup.TtlManager = new TTLManager(SettingsHelper.Read("TimeToLive", 60 * 60 * 1000));
                Startup.TtlManager.Start();
            }
        }
    }
}